package com.tcoiss.log.service.impl;


import com.tcoiss.common.component.api.RemoteApiService;
import com.tcoiss.common.component.api.RemoteDataFactoryService;
import com.tcoiss.common.core.enums.ErroEnums;
import com.tcoiss.common.core.exception.CustomException;
import com.tcoiss.common.core.utils.DateUtils;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.log.domain.SchemeLog;
import com.tcoiss.log.mapper.SchemeLogMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.tcoiss.log.service.ISchemeExecuteLogService;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 方案执行日志Service业务层处理
 *
 * @author zw
 * @date 2021-05-06
 */
@Service
public class SchemeLogServiceImpl extends ServiceImpl<SchemeLogMapper, SchemeLog> implements ISchemeExecuteLogService {

    @Resource
    RemoteDataFactoryService remoteDataFactoryService;

    @Resource
    private RemoteApiService remoteApiService;

    @Override
    public List<SchemeLog> queryList(SchemeLog schemeLog) {
        LambdaQueryWrapper<SchemeLog> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(schemeLog.getSchemeName())) {
            lqw.like(SchemeLog::getSchemeName, schemeLog.getSchemeName());
        }
        if (ObjectUtils.isNotEmpty(schemeLog.getExecuteNumber())) {
            lqw.eq(SchemeLog::getExecuteNumber, schemeLog.getExecuteNumber());
        }
        if (schemeLog.getExecuteType() != null) {
            lqw.eq(SchemeLog::getExecuteType, schemeLog.getExecuteType());
        }
        if (schemeLog.getExecuteStatus() != null) {
            lqw.eq(SchemeLog::getExecuteStatus, schemeLog.getExecuteStatus());
        }
        if(schemeLog.getLogTime() != null){
            lqw.ge(SchemeLog::getLogTime, new Date(DateUtils.getDailyStartTime(schemeLog.getLogTime().getTime())));
            lqw.le(SchemeLog::getLogTime,new Date(DateUtils.getDailyEndTime(schemeLog.getLogTime().getTime())));
        }
        if(schemeLog.getLogTime()!=null){
            lqw.ge(SchemeLog::getLogTime,schemeLog.getLogTime());
        }
        lqw.orderByDesc(SchemeLog::getLogTime);
        List<SchemeLog> logs = this.list(lqw);
//        for (SchemeLogVo log : logs){
//            queryScheme(log);
//        }
        return logs;
    }

//    /**
//     * 获取作业数量与方案名称
//     */
//    @Override
//    public SchemeLogVo queryScheme(SchemeLogVo sel) {
//        SchemeVO vo = new SchemeVO();
//        vo.setExecuteNumber(sel.getExecuteNumber());
//        vo.setExecuteType(sel.getExecuteType());
//        Map<String, Object> map = remoteDataFactoryService.queryScheme(vo);
//        if (map != null && map.size() > 0) {
//            sel.setWorkNum((Integer) map.get("workNum"));
//            sel.setSchemeName(map.get("schemeName").toString());
//        }
//        return sel;
//    }

    @Override
    public Date getSchemeLogNewTime(String executeNumber) {
        LambdaQueryWrapper<SchemeLog> lqw = Wrappers.lambdaQuery();
        if (executeNumber != null) {
            lqw.eq(SchemeLog::getExecuteNumber, executeNumber);
        }
        lqw.eq(SchemeLog::getExecuteStatus, 0);
        lqw.orderByDesc(SchemeLog::getLogTime);
        List<SchemeLog> logs = this.list(lqw);
        if(logs!=null&&logs.size()>0){
            return logs.get(0).getLogTime();
        }else{
            return null;
        }

    }

    @Override
    public boolean checkSchemeLog() {
        SchemeLog schemeExecuteLog = new SchemeLog();
        schemeExecuteLog.setExecuteStatus(1); // 设置失败状态
        schemeExecuteLog.setLogTime(DateUtils.getDayBegin());//设置日志日期
        List<SchemeLog> list = this.queryList(schemeExecuteLog);
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> message1 = new HashMap<>();
            List<Map<String,Object>> articles1 = new ArrayList<>();
            Map<String,Object> article = new HashMap<>();
            SchemeLog sel = list.get(i);
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            String description1 = "编号："+sel.getId()+"\n"+
                    "方案执行日期："+ sdf.format(sel.getLogTime()) + "\n" +
                    "原因分析："+ sel.getErrorMsg() + "\n";
            article.put("title","方案执行异常");
            article.put("description",description1);
            article.put("url","http://192.168.0.80:8088/#/log/jclog/schemeLog");
            articles1.add(article);
            message1.put("jobNo","2110033");
            message1.put("articles",articles1);
            AjaxResult ajaxResult = remoteApiService.sendWxMessage(message1);
            int code2 = -1;
            if(Integer.valueOf(ajaxResult.get("code").toString())==200){
                code2 = Integer.valueOf(ajaxResult.get("data").toString());
            }
            if(code2!=0){
                throw new CustomException(ErroEnums.COMERRO.getCode(),"发送消息异常",null);
            }
        }
        return true;
    }


}
